package com.ziyueyuwen.xiniao.controller.question;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ao.QuestionDataSaveAO;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.query.QuestionQuery;
import com.ziyueyuwen.xiniao.bean.question.ZyQuestionData;
import com.ziyueyuwen.xiniao.bean.vo.ZyQuestionDataVO;
import com.ziyueyuwen.xiniao.controller.question.point.PointController;
import com.ziyueyuwen.xiniao.service.QuestionDataService;
import com.ziyueyuwen.xiniao.service.SubjectQuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;

/**
 * @author wancheng  on 2019/1/10.
 */
@Controller
@RequestMapping("/question")
public class QuestionController {

    private final static Logger logger = LoggerFactory.getLogger(PointController.class);

    @Autowired
    QuestionDataService questionDataService;
    @Autowired
    SubjectQuestionService subjectQuestionService;

    @RequestMapping(value = "/save_question.do",method = RequestMethod.POST)
    public void saveQuestion(HttpServletRequest request,
                             HttpServletResponse response,
                             @Validated QuestionDataSaveAO questionData) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("题目保存");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            ZyQuestionData data = new ZyQuestionData();
            data.setCreateTime(new Date());
            data.setDifficultLevel(questionData.getDifficultLevel());
            data.setGrade(questionData.getGrade());
            data.setTitle(questionData.getTitle());
            data.setLessonModule(questionData.getLessonModule());
            data.setQuestionData(questionData.getQuestionData());
            data.setType(questionData.getType());
            data.setUpdateUserAccount(userAccount);

            if (questionData.getCourseId() == null){
                questionDataService.insert(data,questionData.getPoints());
            }else{
                questionDataService.insert(data, questionData.getPoints(), questionData.getCourseId(),
                        questionData.getLessonIndex(),questionData.getQuestionGroup(), questionData.getAnswerOrder());

            }
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_question.do",method = RequestMethod.POST)
    public void updateQuestion(HttpServletRequest request,
                               HttpServletResponse response,
                               @Validated QuestionDataSaveAO questionData) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("题目修改");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            if (questionData.getId() == null) {
                throw new Exception("id为空");
            }
            ZyQuestionData data = new ZyQuestionData();
            data.setId(questionData.getId());
            data.setTitle(questionData.getTitle());
            data.setDifficultLevel(questionData.getDifficultLevel());
            data.setGrade(questionData.getGrade());
            data.setLessonModule(questionData.getLessonModule());
            data.setQuestionData(questionData.getQuestionData());
            data.setType(questionData.getType());
            data.setUpdateUserAccount(userAccount);

            if (questionData.getCourseId() == null){
                questionDataService.updateByPrimaryKey(data,questionData.getPoints());
            }else{
                questionDataService.updateSyncCourse(data, questionData.getPoints(), questionData.getCourseId(), questionData.getLessonIndex(), questionData.getAnswerOrder());
            }
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/delete_question.do",method = RequestMethod.POST)
    public void deleteQuestion(HttpServletRequest request,
                               HttpServletResponse response,
                               Integer id) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("题目删除");
        try {
            if (id == null) {
                throw new Exception("id为空");
            }
            questionDataService.deleteByPrimaryKey(id);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
    @RequestMapping(value = "/get_question_by_id.do",method = RequestMethod.POST)
    public void selectQuestion(HttpServletRequest request,
                               HttpServletResponse response,
                               Integer id) {
        HttpResContent<ZyQuestionDataVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("根据id或获取数据");
        try {
            if (id == null){
                throw new Exception("questionDataId 不能为空");
            }
            ZyQuestionDataVO data = questionDataService.getByPrimaryKey(id);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
    @RequestMapping(value = "/get_question.do",method = RequestMethod.POST)
    public void selectQuestion(HttpServletRequest request,
                               HttpServletResponse response,
                               QuestionQuery query) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("题目查询");
        try {
            PageData data = questionDataService.getQuestion(query);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
